//#include "uidetail.h"
//#include "ui_uidetail.h"

//#include <QDebug>
//#include <QLabel>
//#include <QProgressBar>
//#include <QPushButton>
//#include <QVBoxLayout>
//#include <QLineEdit>
//#include <QChartView>
//#include <QtCharts>

////static const int daySecondsCount = 86400; // 24 * 60 * 60
////static const int daySecond0930 = 34200;
////static const int daySecond1130 = 41400;
////static const int daySecond1300 = 46800;
////static const int daySecond1500 = 54000;
////static const int utc8SecondCount = 28800;

//static QString const buttonWidgetObjectName = "buttonWidget";
//static QString const chartViewWidgetObjectName = "chartViewWidget";
//static QString const moreWidgetObjectName = "moreWidget";

//static QString const todayValuationQString = "Valuation";
//static QString const sevenDaysEarnQString = "7 Days";
//static QString const oneMonthEarnQString = "1 Month";
//static QString const sixMonthsEarnQString = "6 Months";
//static QString const oneYearEarnQString = "1 Year";
//static QString const threeYearsEarnQString = "3 Years";

//static QString const warningGetFundValuationFailedQString = "Get valuation failed, try it again.";

//static QPen redPen(Qt::red), greenPen(Qt::green);

//struct __chartViewData {
//    QChart *lineChart;
//    QChartView *chartView;
////    QValueAxis *xAxis;
//    QDateTimeAxis *xAxis;
//    QValueAxis *yAxis;
//    QLineSeries *spLineSeries;
//};

/////////////////////////////////////////////////////////////////////////
/////*
////    A legal valuation must start with 9:30 to 11:30 and from 13:00 to 15:00
////    return true if the timestamp is in here otherwise return false
////*/
////static bool getValuationTimeEnable(int timestamp) {
////    timestamp += utc8SecondCount;
////    int timestampOfToday = timestamp - (timestamp / daySecondsCount) * daySecondsCount;
////    if(timestampOfToday >= daySecond0930 && timestampOfToday <= daySecond1500) {
////        if(timestampOfToday <= daySecond1130 || timestampOfToday >= daySecond1300) {
////            return true;
////        }
////    }
////    return false;
////}

//void UIDetail::showTodayValuationByList() {
//    double min_xAxis = 0, max_xAxis = 10;
//    double min_yAxis = 0, max_yAxis = 10;

//    qDebug() << "valuationList->size() " << valuationList->size() << endl;

//    if(valuationList != NULL && valuationList->size() > 0) {
//        min_xAxis = valuationList->at(0).timestamp;
//        max_xAxis = valuationList->at(valuationList->size() - 1).timestamp;
//        min_yAxis = valuationList->at(0).rate;
//        max_yAxis = valuationList->at(0).rate;

//        oscillograph->xAxis->setFormat("hh:mm");

//        if(valuationList->at(valuationList->size() - 1).rate < 0) {
//            oscillograph->spLineSeries->setPen(greenPen);
//        } else {
//            oscillograph->spLineSeries->setPen(redPen);
//        }

//        for(int i = 0; i < valuationList->size(); i++) {
//            valuation_data data = valuationList->at(i);
//            QDateTime data_xAxisTime = QDateTime::fromTime_t(data.timestamp);
//            oscillograph->spLineSeries->append(data_xAxisTime.toMSecsSinceEpoch(), data.rate);
//            min_yAxis = min_yAxis < data.rate ? min_yAxis : data.rate;
//            max_yAxis = max_yAxis > data.rate ? max_yAxis : data.rate;
//        }

//        // Edit x axis
//        QDateTime min_xAxisTime, max_xAxisTime;
//        if(max_xAxis <= min_xAxis) {
//            max_xAxis = min_xAxis + 10;
//        }
//        min_xAxisTime = QDateTime::fromTime_t(min_xAxis);
//        max_xAxisTime = QDateTime::fromTime_t(max_xAxis);
//        oscillograph->xAxis->setRange(min_xAxisTime, max_xAxisTime);

//        int xCount = valuationList->size();
//        xCount = xCount < 4 ? 4 : xCount;
//        xCount = xCount > 11 ? 11 : xCount;
//        oscillograph->xAxis->setTickCount(xCount);

//        // Edit y axis
//        double rate_reserve = (max_yAxis - min_yAxis) / 4;
//        rate_reserve = 0; //rate_reserve > 1 ? 1 : rate_reserve;
//        if(max_yAxis <= min_yAxis) {
//            max_yAxis = min_yAxis + 0.5;
//        }
//        oscillograph->yAxis->setRange(min_yAxis - rate_reserve, max_yAxis + rate_reserve);
//    }
//}

////void UIDetail::showTodayValuation() {
////    oscillograph->spLineSeries->clear();

////    if(valuationList == NULL) {
////        valuationList = pyFund->getValuationData(codeRec);
////        if(valuationList != NULL) {
////            showTodayValuationByList(valuationList, oscillograph);
////        } else {
////            QMessageBox::information(NULL, "Warning", warningGetFundValuationFailedQString, QMessageBox::Ok);
////            lastQPushButtonQString = QString("NULL");
////        }
////    } else {
////        showTodayValuationByList(valuationList, oscillograph);
////    }
////}
//void UIDetail::showTodayValuation() {
//    qDebug() << "showTodayValuation";
//    oscillograph->spLineSeries->clear();
////    if(valuationList == NULL) {
////        valuation_spider->ask(codeRec);
////    } else {
////        // TODO: check weather need to update
////        showTodayValuationByList();
////    }
//    valuation_spider->ask(codeRec);
//}

////static void showsevenDaysEarn() {

////}

//void UIDetail::buttonLayoutInit() {
//    QWidget *buttonWidget = new QWidget();
//    buttonWidget->setObjectName(buttonWidgetObjectName);
//    QHBoxLayout *buttonLayout = new QHBoxLayout(buttonWidget);

//    QPushButton *buttonResent = new QPushButton(todayValuationQString);
//    QPushButton *button7Days = new QPushButton(sevenDaysEarnQString);
//    QPushButton *button1Month = new QPushButton(oneMonthEarnQString);
//    QPushButton *button6Months = new QPushButton(sixMonthsEarnQString);
//    QPushButton *button1Year = new QPushButton(oneYearEarnQString);
//    QPushButton *button3Years = new QPushButton(threeYearsEarnQString);

//    buttonLayout->addWidget(buttonResent);
//    buttonLayout->addWidget(button7Days);
//    buttonLayout->addWidget(button1Month);
//    buttonLayout->addWidget(button6Months);
//    buttonLayout->addWidget(button1Year);
//    buttonLayout->addWidget(button3Years);
//    buttonLayout->setContentsMargins(0, 0, 0, 0);
//    buttonWidget->setLayout(buttonLayout);

//    ui->verticalLayout->addWidget(buttonWidget);
//}

//void UIDetail::buttonLayoutDelete(QLayoutItem *item) {
//    QWidget *widget = item->widget();
//    QHBoxLayout *layout = (QHBoxLayout *)widget->layout();
//    QLayoutItem *litem = nullptr;
//    while((litem = layout->itemAt(0)) != nullptr) {
//        layout->removeItem(litem);
//        delete litem->widget();
//        delete litem;
//    }

//    delete layout;
//    delete widget;
//}

//void UIDetail::chartViewLayoutInit() {
//    oscillograph->lineChart = new QChart();
//    oscillograph->chartView = new QChartView();
////    oscillograph->xAxis = new QValueAxis();
//    oscillograph->xAxis = new QDateTimeAxis();
//    oscillograph->yAxis = new QValueAxis();
//    oscillograph->spLineSeries = new QLineSeries();//new QSplineSeries();

//    redPen.setWidth(1);
//    greenPen.setWidth(1);

//    oscillograph->spLineSeries->setPen(redPen);

//    oscillograph->lineChart->addSeries(oscillograph->spLineSeries);
//    oscillograph->lineChart->addAxis(oscillograph->xAxis,Qt::AlignBottom);
//    oscillograph->lineChart->addAxis(oscillograph->yAxis,Qt::AlignLeft);

//    oscillograph->spLineSeries->attachAxis(oscillograph->xAxis);
//    oscillograph->spLineSeries->attachAxis(oscillograph->yAxis);

////    oscillograph->xAxis->setTickCount(11);
////    oscillograph->xAxis->setRange(0, 10);
////    oscillograph->yAxis->setRange(0, 10);

//    oscillograph->lineChart->setTitle(todayValuationQString);
//    oscillograph->lineChart->legend()->hide();
//    oscillograph->lineChart->setAnimationOptions(QChart::AllAnimations);

//    oscillograph->chartView->setObjectName(chartViewWidgetObjectName);
//    oscillograph->chartView->setChart(oscillograph->lineChart);
//    oscillograph->chartView->setRenderHint(QPainter::Antialiasing);
//    oscillograph->chartView->setContentsMargins(0, 0, 0, 0);
//    ui->verticalLayout->addWidget(oscillograph->chartView);
//}

//void UIDetail::chartViewLayoutDelete(chartViewData *oscillograph) {
//    delete oscillograph->lineChart;
//    delete oscillograph->chartView;
//    delete oscillograph->xAxis;
//    delete oscillograph->yAxis;
//    delete oscillograph->spLineSeries;
//}

//void UIDetail::moreTailLayoutInit() {
//    QWidget *moreWidget = new QWidget();
//    moreWidget->setObjectName(moreWidgetObjectName);
//    QHBoxLayout *moreLayout = new QHBoxLayout(moreWidget);
//    QPushButton *buttonMore = new QPushButton("More");
//    moreLayout->addWidget(buttonMore);
//    moreLayout->setContentsMargins(0, 0, 0, 0);
//    moreWidget->setLayout(moreLayout);

//    ui->verticalLayout->addWidget(moreWidget);
//}

//void UIDetail::moreTailLayoutDelete(QLayoutItem *item) {
//    QWidget *widget = item->widget();
//    QHBoxLayout *layout = (QHBoxLayout *)widget->layout();
//    QLayoutItem *litem = nullptr;
//    while((litem = layout->itemAt(0)) != nullptr) {
//        layout->removeItem(litem);
//        delete litem->widget();
//        delete litem;
//    }

//    delete layout;
//    delete widget;
//}

/////////////////////////////////////////////////////////////////////////

//void UIDetail::initAllItem() {
//    buttonLayoutInit();
//    chartViewLayoutInit();
//    moreTailLayoutInit();
//}

//void UIDetail::deleteAllItem() {
//    QLayoutItem *item = nullptr;
//    while( (item = ui->verticalLayout->itemAt(0)) != nullptr) {
//        QString obName = item->widget()->objectName();
//        if(obName.compare(buttonWidgetObjectName) == 0) {
//            buttonLayoutDelete(item);
//        } else if(obName.compare(chartViewWidgetObjectName) == 0) {
//            chartViewLayoutDelete(oscillograph);
//        } else if(obName.compare(moreWidgetObjectName) == 0) {
//            moreTailLayoutDelete(item);
//        }

//        ui->verticalLayout->removeItem(item);
//        delete item;
//    }
//}

/////////////////////////////////////////////////////////////////////////
//void UIDetail::pushButtonClicked() {
//    QString text = ((QPushButton *)sender())->text();
//    qDebug() << text;
////    if(lastQPushButtonQString.compare(text) == 0) {
////        return;
////    }

//    oscillograph->lineChart->setTitle(text);
//    lastQPushButtonQString = text;

//    if(text.compare(todayValuationQString) == 0) {
//        showTodayValuation();
//    } else if(text.compare(sevenDaysEarnQString) == 0) {

//    } else if(text.compare(oneMonthEarnQString) == 0) {

//    } else if(text.compare(sixMonthsEarnQString) == 0) {

//    } else if(text.compare(oneYearEarnQString) == 0) {

//    } else if(text.compare(threeYearsEarnQString) == 0) {

//    }
//}

//UIDetail::UIDetail(QString code, QWidget *parent) :
//    QWidget(parent),
//    ui(new Ui::UIDetail)
//{
//    ui->setupUi(this);
//    codeRec = code;
//    oscillograph = new chartViewData();

//    initAllItem();
//    bondPushButtonEvent();
//    lastQPushButtonQString = QString("NULL");

////    pyFund = new FundData();
//    valuation_spider = new FundDB_spider();
//    connect(valuation_spider, SIGNAL(finished(bool)), this, SLOT(valuationGotten(bool)));

//    lastQPushButtonQString = todayValuationQString;
//    showTodayValuation();
//}

//UIDetail::~UIDetail()
//{
//    if(valuationList != NULL) {
//        valuationList->clear();
//        delete valuationList;
//    }
//    deleteAllItem();
//    delete oscillograph;
//    delete valuation_spider;
//    delete ui;
//}

//void UIDetail::bondPushButtonEvent() {
//    QLayoutItem *item = nullptr;
//    // Find button layout first
//    while( (item = ui->verticalLayout->itemAt(0)) != nullptr) {
//        QString obName = item->widget()->objectName();
//        if(obName.compare(buttonWidgetObjectName) == 0) {
//            break;
//        }
//    }

//    if(item != nullptr) {
//        QWidget *widget = item->widget();
//        QHBoxLayout *layout = (QHBoxLayout *)widget->layout();

//        for(int i = 0;i < layout->count(); i++) {
//            QLayoutItem *litem = layout->itemAt(i);
//            QPushButton *button = (QPushButton *)(litem->widget());
//            connect(button, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
//        }
//    }
//}

//void UIDetail::valuationGotten(bool rslt) {
//    if(rslt) {
//        valuationList = valuation_spider->get_valuationList();
//        showTodayValuationByList();
//    } else {
//        qDebug() <<"Get valuation failed";
//    }
//}
